﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;
using System.Data;
using System.Configuration;

namespace KPIS.GERP.GHRMS.DAL
{
    public class TrainingScholarshipExpenseDAL
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public DataSet GetData(Nullable<int> exp_seq)
        {
            DataSet ds = new DataSet();
            dbManager.Open();
            dbManager.CreateParameters(2);
            dbManager.AddParameters(0, "@exp_seq", exp_seq);
            dbManager.AddParameters(1, "@record_status", SystemInfo._ActiveRecordStatus);

            string sql = "SELECT hrm_scholarships_expenses.exp_seq as exp_seq"
                        + ", hrm_scholarships_expenses.exp_descr as exp_descr"
                        + " FROM hrm_scholarships_expenses"
                        + " WHERE hrm_scholarships_expenses.record_status = @record_status"
                        + (exp_seq > 0 ? " AND hrm_scholarships_expenses.exp_seq = @exp_seq " : "");

            ds = dbManager.ExecuteDataSet(CommandType.Text, sql);
            dbManager.Dispose();
            return ds;
        }
        public void Insert(string exp_descr, int created_by)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@exp_descr", exp_descr);
                dbManager.AddParameters(1, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(2, "@created_by", created_by);
                dbManager.AddParameters(3, "@created_when", DateTime.Today);

                strSQL = "INSERT INTO hrm_scholarships_expenses"
                        + " (exp_descr, record_status, created_by, created_when)"
                        + " VALUES (@exp_descr, @record_status, @created_by, @created_when)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(int exp_seq, string exp_descr, int updated_by)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@exp_seq", exp_seq);
                dbManager.AddParameters(1, "@exp_descr", exp_descr);
                dbManager.AddParameters(2, "@updated_by", updated_by);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE hrm_scholarships_expenses"
                    + " SET exp_descr = @exp_descr"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE exp_seq = @exp_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int exp_seq, int updated_by)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@exp_seq", exp_seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", updated_by);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE hrm_scholarships_expenses"
                    + " SET record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE exp_seq = @exp_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
